/**
 * 
 */
package com.tvshows.front.view.dialog;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Monitor;
import org.eclipse.swt.widgets.Shell;

import com.tvshows.front.factory.DialogFactory;
import com.tvshows.front.factory.TypeUI;
import com.tvshows.front.ui.FrontUI;
import com.tvshows.front.ui.dialog.ActionsUI;
import com.tvshows.front.ui.dialog.DialogUI;

/**
 * @author tangi
 * <p>
 * Implementation of {@link ActionsUI}
 * </p>
 */
public class ActionsView
    implements ActionsUI
{

    /**
     * The shell
     */
    private Shell shell;

    /**
     * sets the modify mode
     */
    private boolean modify;

    /**
     * @see com.tvshows.front.ui.dialog.ActionsUI#openShell(com.tvshows.front.ui.FrontUI)
     */
    @Override
    public void openShell( FrontUI view )
    {
        shell = new Shell( view.getShell() );
        DialogUI frame = new DialogAddFavoriteView( shell, SWT.BORDER | SWT.RESIZE );
        ( (DialogAddFavoriteView) frame ).setModify( modify );
        ( (DialogAddFavoriteView) frame ).setSize( 330, 280 );

        configureShell( frame );
    }

    /**
     * @see com.tvshows.front.ui.dialog.ActionsUI#openMenuItem(com.tvshows.front.ui.FrontUI,
     * com.tvshows.front.factory.TypeUI)
     */
    public void openMenuItem( FrontUI view, TypeUI typeUI )
    {
        DialogUI dialog = null;

        if ( typeUI == TypeUI.LOG_VIEW )
        {
            shell = new Shell( view.getShell() );
            dialog = DialogFactory.createDialogUI( TypeUI.LOG_VIEW, shell, 0, 700, 450 );
        }

        if ( typeUI == TypeUI.OPTION_VIEW )
        {
            shell = new Shell( view.getShell() );
            dialog = DialogFactory.createDialogUI( TypeUI.OPTION_VIEW, shell, 0, 700, 450 );
        }
        configureShell( dialog );
        shell.pack();
    }

    /**
     * Configures the shell
     * 
     * @param view the view
     */
    private void configureShell( DialogUI view )
    {
        shell.setSize( ( (DialogUI) view ).getSize() );

        /** Center the shell **/
        Monitor primary = shell.getDisplay().getPrimaryMonitor();
        Rectangle bounds = primary.getBounds();
        Rectangle rect = shell.getBounds();

        int x = bounds.x + ( bounds.width - rect.width ) / 2;
        int y = bounds.y + ( bounds.height - rect.height ) / 2;

        shell.setLocation( x, y );

        shell.open();
    }

    /**
     * @see com.tvshows.front.ui.FrontUI#getShell()
     */
    @Override
    public Shell getShell()
    {
        return shell;
    }

    /**
     * @see com.tvshows.front.ui.dialog.ActionsUI#setModify(boolean)
     */
    @Override
    public void setModify( boolean modify )
    {
        this.modify = modify;
    }

    @Override
    public void initFormData()
    {
        // unused
    }

    @Override
    public void initComposites()
    {
        // unused

    }

    /**
     * @see com.tvshows.front.ui.dialog.ActionsUI#openJobRunningPopUp(com.tvshows.front.ui.FrontUI)
     */
    @Override
    public void openJobRunningPopUp( FrontUI view )
    {
        MessageBox box = new MessageBox( view.getShell(), SWT.ICON_INFORMATION );
        box.setMessage( "Rafraichissement en cours, pas de suppression ni d'ajout" );

        box.open();

    }

}
